<?php
declare(strict_types=1);

namespace App\Logic\User\Service\User;

use App\Exceptions\DbException;
use App\Library\SnowFlakeId;
use App\Logic\User\Repository\User\AuthorFansRepository;
use App\Logic\User\Service\BaseService;
use App\Logic\User\Service\UserServiceInterface;
use Closure;

class AuthorFansService extends BaseService implements UserServiceInterface
{

    public static function searchWhere(array $requestParams): Closure
    {
        return function ($query) use ($requestParams) {
            extract($requestParams);
            if (!empty($user_uid)) {
                $query->where("user_uid", "=", $user_uid);
            }
        };
    }

    /**
     * 用户关注列表
     * @param array $requestParams
     * @return array
     */
    public function serviceSelect(array $requestParams): array
    {
        $requestParams["user_uid"] = $this->getUserUid();
        return (new AuthorFansRepository)->repositorySelect(self::searchWhere($requestParams),
            (int)($requestParams["size"]),
            ["author_uid", "created_at as attention_time"]
        );
    }

    /**
     * 关注作者
     * @param array $requestParams
     * @return array
     * @throws DbException
     */
    public function serviceCreate(array $requestParams): array
    {
        $requestParams["uid"] = SnowFlakeId::getId();
        $requestParams["user_uid"] = $this->getUserUid();
        if ((new AuthorFansRepository())->repositoryCreate($requestParams)) {
            return ["code" => 0, "msg" => "关注成功"];
        }
        return ["code" => 1, "msg" => "关注失败"];
    }

    public function serviceUpdate(array $requestParams): array
    {
        return [];
    }

    public function serviceDelete(array $requestParams): int
    {
        return 0;
    }

    public function serviceFind(array $requestParams): array
    {
        return [];
    }
}
